﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class RoundDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static RoundDAO()
        {
            XmlConfigurator.Configure();
        }

        public static List<RoundDTO> GetRoundList()
        {
            try
            {
                List<RoundDTO> lstRound = new List<RoundDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                var query = from Round in context.ROUND_LIST
                            select Round;

                foreach (var item in query)
                {
                    RoundDTO dtoRound = new RoundDTO();
                    dtoRound = Mapper.Map<ROUND, RoundDTO>(item);
                    lstRound.Add(dtoRound);
                }
                return lstRound;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static RoundDTO FindRound(RoundDTO dtoRound, string searchOption)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                IQueryable<ROUND> query = null;

                if (searchOption == RoundColumns.ROUND_ID)
                {
                    query = from Round in context.ROUND_LIST
                            where Round.ROUND_ID == dtoRound.ROUND_ID
                            select Round;
                }
                else if (searchOption == RoundColumns.ROUND_NAME)
                {
                    query = from Round in context.ROUND_LIST
                            where Round.ROUND_NAME == dtoRound.ROUND_NAME
                            select Round;
                }

                if (query == null)
                {
                    logger.Debug("Chưa hỗ trợ tìm kiếm theo " + searchOption);
                    return null;
                }

                if (query.Count() == 0)
                    return null;

                RoundDTO Result = new RoundDTO();
                ROUND round = query.First();
                Result = Mapper.Map<ROUND, RoundDTO>(round);
                return Result;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static bool AddNewRound(RoundDTO dtoRound)
        {
            try
            {
                ROUND obj = new ROUND();
                //Mapping                
                obj.START_DATE = dtoRound.START_DATE;
                obj.END_DATE = dtoRound.END_DATE;
                obj.ROUND_NAME = dtoRound.ROUND_NAME;                
                
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                context.AddToROUND_LIST(obj);
                context.SaveChanges(true);

                dtoRound .ROUND_ID  = obj.ROUND_ID ;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool UpdateRound(RoundDTO dtoRound)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                ROUND obj = context.ROUND_LIST.First(r => r.ROUND_ID == dtoRound.ROUND_ID);

                obj.ROUND_NAME = dtoRound.ROUND_NAME;
                obj.START_DATE = dtoRound.START_DATE;
                obj.END_DATE = dtoRound.END_DATE;

                context.SaveChanges(true);
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteARound(RoundDTO dtoRound)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                ROUND obj = context.ROUND_LIST.First(r => r.ROUND_ID == dtoRound.ROUND_ID);
                context.DeleteObject(obj);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAllRound()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from round in context.ROUND_LIST 
                             select round);
                foreach (var item in query)
                {
                    context.DeleteObject(item);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }


    }
}
